﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Hockey.UILayer.Controls
{
    /// <summary>
    /// Circle progress rendering class
    /// TO-DO: make internal
    /// </summary>
    public class ProgressCircle: Control
    {
        public static readonly DependencyProperty ProgressProperty;

        static ProgressCircle()
        {
            // register dependency property
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
            metadata.AffectsRender = true;
            metadata.Inherits = true;
            metadata.DefaultValue = 0;
            metadata.DefaultUpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;

            ProgressProperty = DependencyProperty.Register("Progress", typeof(int), typeof(ProgressCircle), metadata);
        }


        /// <summary>
        /// Rendering method
        /// </summary>
        /// <param name="drawingContext"></param>
        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            // calculate center and radius
            double centerX = RenderSize.Width / 2;
            double centerY = RenderSize.Height / 2;

            double radiusX = centerX;
            double radiusY = centerY;

            // create background brushes and pen
            LinearGradientBrush bckBrush = new LinearGradientBrush();
            bckBrush.GradientStops.Add(new GradientStop(Colors.LightGray, 0));
            bckBrush.GradientStops.Add(new GradientStop(Colors.DarkGray, 1));
            Pen pen = new Pen(Brushes.Black, 3);

            // draw background circle
            drawingContext.DrawEllipse(bckBrush, pen, new Point(centerX, centerY), radiusX, radiusY);

             // create the arc geometry
            ////////////////////////////////////

            // arc starting point
            Point start = new Point(centerX, 0);

            // calculate angle
            bool large = false;
            if ((double)Progress > 50d)
                large = true;

            double angle = ((double)Progress) * Math.PI / 50d;
            angle -= Math.PI / 2;

            Point endArc = new Point(centerX, centerY) + new Vector(Math.Cos(angle), Math.Sin(angle)) * radiusX;

            // create the arc segment
            List<PathSegment> arcSegment = new List<PathSegment>();
            arcSegment.Add(new LineSegment(start, false));
            arcSegment.Add(new ArcSegment(endArc, new Size(radiusX, radiusY), angle, large, SweepDirection.Clockwise, false));

            // create the path
            List<PathFigure> arcPath = new List<PathFigure>();
            arcPath.Add(new PathFigure(new Point(centerX, centerY), arcSegment, true));

            // create geometry
            Geometry arc = new PathGeometry(arcPath);

            // create arc brush and pen
            LinearGradientBrush brush = new LinearGradientBrush();
            brush.GradientStops.Add(new GradientStop(Colors.Yellow, 0));
            brush.GradientStops.Add(new GradientStop(Colors.Green, 1));
           
            // draw arc
            drawingContext.DrawGeometry(brush, new Pen(), arc);        

            base.OnRender(drawingContext);
        }


        /// <summary>
        /// Sets/Gets progress value
        /// </summary>
        public int Progress
        {
            get
            {
                return (int)GetValue(ProgressProperty);
            }
            set
            {
                SetValue(ProgressProperty, value);
            }                
        }

    }
}
